#include "common/api/server.h"
#include "common/api/server_helper.hpp"

#include "api_controller.h"

#include "login_handler.h"

namespace Api
{

LoginHandler::LoginHandler(
    crow::App<>& crowApplication,
    ApiController* controller
)
    : m_controller(controller)
{
    ADD_CROW_ROUTE_LOGIN(
        crowApplication,
        [this](
            const std::string& userLogin,
            const std::string& password
        ) -> AuthResult
        {
            AuthResult result;
            result.m_sessionUuid = m_controller->permissionManager()->login(userLogin, password);

            if (auto user = m_controller->dataController()->userByLogin(userLogin))
                result.m_userId = user->id();

            m_controller->changesManager().addSession(result.m_sessionUuid);
            return result;
        }
    );
}

} // namespace Api
